package consulTest.common;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import tools.LiporUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static org.apache.http.entity.ContentType.APPLICATION_JSON;

@Slf4j
@Component
public class SignAuthInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {

        try {
            if (response.getStatus() != HttpServletResponse.SC_OK) {
                log.error("sign status not 200");
                writeErrorResp(response);
                return false;
            }

            String timestampStr = request.getHeader("timestamp");
            String secret = request.getHeader("secret");
            String source = request.getHeader("source");

            if (StringUtils.isBlank(timestampStr)) {
                writeErrorResp(response);
                log.error("sign miss timestamp");
                return false;
            }

            if (StringUtils.isBlank(source)) {
                writeErrorResp(response);
                log.error("sign miss source");
                return false;
            }

            long timestampHeader = Long.parseLong(timestampStr);
            long timeMillis = System.currentTimeMillis();

//            //判断来源
//            if (!DataSourceEnum.DATA_SOURCE_ENUM_MAP.containsKey(Integer.parseInt(source))) {
//                writeErrorResp(response);
//                log.error("sign source not identify");
//                return false;
//            }

            //一分钟之内
            if (Math.abs(timeMillis - timestampHeader) > 60000) {
                writeErrorResp(response);
                log.error("sign time out");
                return false;
            }

//            if (!MD5Util.verify(timestampStr, secret)) {
//                writeErrorResp(response);
//                log.error("sign secret error");
//                return false;
//            }
            return true;
        } catch (Exception e) {
            try {
                writeErrorResp(response);
            } catch (IOException exception) {
                log.error("sign verify error", e);
            }
            log.error("sign verify error", e);
        }
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {

    }

    private void writeErrorResp(HttpServletResponse response) throws IOException {
        Response<Object> resp = Response.fail(401, "鉴权失败");
        response.setContentType(APPLICATION_JSON.toString());
        response.getWriter().write(LiporUtils.objectToJson(resp));
        response.flushBuffer();
    }

}
